package com.hibernate.test;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;

import com.hibernate.test.simple.Event;
import com.hibernate.test.simple.Person;
import com.hibernate.test.util.HibernateUtil;

public class EventManager {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 EventManager mgr = new EventManager();
		 
		 mgr.createAndSaveEvent("MyEvent3", new Date());
		 List<Event> events = mgr.listEvents();
		 for (int i = 0; i < events.size(); i++) {
		     Event theEvent = (Event) events.get(i);
		     System.out.println(
		         "Event: " + theEvent.getTitle() + " Time: " + theEvent.getDate()
		      );
		 }
		 
		 Long eventId = mgr.createAndSaveEvent("My Event22", new Date());
		 Long personId = mgr.createAndSavePerson("Foo1", "Bar1");
		 mgr.addPersonToEvent(personId, eventId);
		 System.out.println("Added person " + personId + " to event " + eventId);

		 HibernateUtil.getSessionFactory().close();
		 
	}
	
	private List<Event> listEvents() {
	    Session session = HibernateUtil.buildSessionFactory().getCurrentSession();
	    session.beginTransaction();
	    
	    List<Event> result = session.createQuery("from Event").list();
	    
	    session.getTransaction().commit();
	    
	    return result;
	}
	private Long createAndSavePerson(String firstname, String lastname){
		Session session = HibernateUtil.buildSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Person p = new Person();
		p.setFirstname(firstname);
		p.setLastname(lastname);
		session.save(p);
		
		long pid = p.getId();
		
		session.getTransaction().commit();
		
		return pid;
	}
	/**
	 * Event add/save method
	 * @param title
	 * @param theDate
	 */
	private long createAndSaveEvent(String title, Date theDate) {
		Session session = HibernateUtil.buildSessionFactory().getCurrentSession();
		
		session.beginTransaction();
		
		Event theEvent = new Event();
		theEvent.setTitle(title);
		theEvent.setDate(theDate);
		session.save(theEvent);
		long eid = theEvent.getId();
		
		session.getTransaction().commit();
		
		return eid;
	}
	
	/**
	 * Show how to add a relationship table between two entity table;
	 * @param personId
	 * @param eventId
	 */
	private void addPersonToEvent(Long personId, Long eventId){
		Session session = HibernateUtil.buildSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Person aperson = (Person)session.load(Person.class, personId);
		Event anEvent = (Event)session.load(Event.class, eventId);
		
		aperson.addToEvent(anEvent);
		session.getTransaction().commit();
		
	}

	private void addPersonToEventInDetachMode(Long personId, Long eventId) {
		Session session = HibernateUtil.buildSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Person aPerson = (Person)session.createQuery(
				"select p from Person p left join fetch p.events where p.id = :pid").
				setParameter("pid", personId).uniqueResult();
		
		Event anEvent = (Event)session.load(Event.class, eventId);
		session.getTransaction().commit();
		
		aPerson.addToEvent(anEvent);
		
		Session session2 = HibernateUtil.buildSessionFactory().getCurrentSession();
		session2.beginTransaction();
		
		session2.save(aPerson);
		
		session2.getTransaction().commit();
	}	
	
	private void addEmailToPerson(Long personId, String emailAddress) {
		Session session = HibernateUtil.buildSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Person aPerson = (Person)session.load(Person.class, personId);
		aPerson.getEmailAddresses().add(emailAddress);
		
		session.getTransaction().commit();
		
	}
}
